package docoffice.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import docoffice.doss.Appointment;
import docoffice.doss.Doctor;
import docoffice.doss.Patient;
import docoffice.doss.Patient.insuranceCarrier;
import docoffice.doss.TreatmentRec;

public class MakeApptPanel extends JPanel {
	
	private JLabel title, blank, date, time, reason, doctor, user, blank2;
	
	private JTextField dateText, timeText, reasonText, doctorText, userText;
	
	private JButton confirm;
	
	public MakeApptPanel() {
		title = new JLabel("Schedule an Appointment");
		blank = new JLabel();
		
		date = new JLabel("Schedule a date (MM/DD/YY)");
		time = new JLabel("Schedule a time (HH:MM)");
		reason = new JLabel("Explain your symptoms");
		doctor = new JLabel("Request a Doctor (First Last)");
		user = new JLabel("Please type your username");
		
		dateText = new JTextField(10);
		timeText = new JTextField(10);
		reasonText = new JTextField(10);
		doctorText = new JTextField(10);
		userText = new JTextField(10);
		
		blank2 = new JLabel();
		confirm = new JButton("Schedule Now");
		
		setLayout(new GridLayout(7, 2, 15, 10));
		add(title);
		add(blank);
		add(date);
		add(dateText);
		add(time);
		add(timeText);
		add(reason);
		add(reasonText);
		add(doctor);
		add(doctorText);
		add(user);
		add(userText);
		add(blank2);
		add(confirm);
		
		TextListener testList = new TextListener();
		dateText.addActionListener(testList);
		timeText.addActionListener(testList);
		reasonText.addActionListener(testList);
		doctorText.addActionListener(testList);
		userText.addActionListener(testList);
		confirm.addActionListener(new ConfirmListener());
		
		setPreferredSize(new Dimension(500, 225));
		setBackground(Color.WHITE);
	}
	
	private class TextListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String dateInput = dateText.getText();
			String timeInput = timeText.getText();
			String reasonInput = reasonText.getText();
			String doctorInput = doctorText.getText();
			String usernameInput = userText.getText();
		}
	}
	
	private class ConfirmListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int value = JOptionPane.showConfirmDialog(null, "Confirm scheduling appointment?",
					"Confirm", JOptionPane.YES_NO_OPTION);
			if (value == JOptionPane.YES_OPTION) {
				String dateInput = dateText.getText();
				String timeInput = timeText.getText();
				String reasonInput = reasonText.getText();
				String doctorInput = doctorText.getText();
				Doctor doc = new Doctor(doctorInput, "");
				doc.createUser();
				doc.setUserType("Doctor");
				doc.setUsername(doctorInput);
				doc.createMedicalEmployee();
				doc.updateMedicalEmployee();
				doc.createDoctor();
				String usernameInput = userText.getText();
				Patient patient = new Patient(usernameInput);
				patient.setUsername(usernameInput);
				patient.setInsuranceCarrier(insuranceCarrier.NONE);
				patient.setAddress("");
				patient.setGender("");
				patient.createPatient();
				Appointment appointment = new Appointment(dateInput, timeInput, reasonInput, doc);
				appointment.setPatient(patient);
				TreatmentRec treatment = new TreatmentRec(null, "", "", "", "", null, null, null);
				appointment.createAppointment(dateInput, timeInput, reasonInput, doc, patient, treatment);
				System.out.println("yes");
				removeAll();
				revalidate();
				repaint();
				add(new LoginPanel());
			}
		}
	}

}
